package com.wanxi.controller;

import com.alibaba.fastjson2.JSONObject;
import com.wanxi.dto.ResultDTO;
import com.wanxi.model.NewsType;
import com.wanxi.service.NewsService;
import com.wanxi.vo.NewsVO;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/news")
@CrossOrigin(origins = "http://localhost:8081")
public class NewsController {
    @Autowired
    NewsService newsService;

    @PutMapping("/add")
    public String addNews(NewsVO newsVO,
                          @Param("newsTypeID") String newsTypeID){
        newsVO.setNewsType(NewsType.builder().id(newsTypeID).build());
        ResultDTO resultDTO = newsService.addNews(newsVO);
        return JSONObject.toJSONString(resultDTO);
    }

    @DeleteMapping("/delete")
    public String deleteNews(NewsVO newsVO){
        ResultDTO resultDTO = newsService.deleteNews(newsVO);
        return JSONObject.toJSONString(resultDTO);
    }

    @GetMapping("/findAll")
    public String findAllNews(){
        ResultDTO allNews = newsService.findAllNews();
        return JSONObject.toJSONString(allNews);
    }

    @PostMapping("/findById")
    public String findNewsById(NewsVO newsVO){
        ResultDTO newsById = newsService.findNewsById(newsVO);
        return JSONObject.toJSONString(newsById);
    }

    @GetMapping("/findBySome")
    public String findNewsBySome(NewsVO newsVO){
        ResultDTO newsBySome = newsService.findNewsBySome(newsVO);
        return JSONObject.toJSONString(newsBySome);
    }

    @PostMapping("/updateEnable")
    public String updateNewsEnable(NewsVO newsVO){
        ResultDTO resultDTO = newsService.updateEnable(newsVO);
        return JSONObject.toJSONString(resultDTO);
    }

    @PostMapping("/update")
    public String updateNews(NewsVO newsVO,
                             @Param("newsTypeID") String newsTypeID){
        newsVO.setNewsType(NewsType.builder().id(newsTypeID).build());
        ResultDTO resultDTO = newsService.updateNews(newsVO);
        return JSONObject.toJSONString(resultDTO);
    }
}
